﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BizElements.TemplateEngine;

namespace BizElements.Generator
{
    public partial class ComponentDetailsCtl : UserControl
    {
        ITemplateInputComponent inputComponent;
        ComponentType[] subcomponentTypes;

        public ComponentDetailsCtl()
        {
            InitializeComponent();
        }

        private void ComponentDetailsCtl_Load(object sender, EventArgs e)
        {
            if (this.inputComponent == null || this.subcomponentTypes == null || this.subcomponentTypes.Length == 0)
                this.uiMainTab.Controls.Remove(this.uiSubcomponentsPage);
        }

        public void SetComponentData(ITemplateInputComponent inputComponent, ComponentType[] subcomponentTypes, IEnumerable<ITemplate> allTemplates, IDictionary<string, IDictionary<string, string>> allComponentVariables)
        {
            // For now, we'll support only one subcomponent type. Eg. only fields. Late we may add more tabs.
            this.inputComponent = inputComponent;
            this.subcomponentTypes = subcomponentTypes;

            if (inputComponent == null || subcomponentTypes == null || subcomponentTypes.Length == 0)
            {
                this.uiSubcomponents.SetComponentData(null, new ComponentType(Guid.Empty), null, null);
            }
            else
            {
                this.uiSubcomponents.SetComponentData(inputComponent, subcomponentTypes[0], allTemplates, allComponentVariables);

                string componentName = inputComponent.FullName;
                var varDefinitions = TemplateUtil.GetApplicableComponentVariables(inputComponent, allTemplates);
                var varValues = TemplateUtil.GetComponentVariableValues(allComponentVariables, componentName);
                this.uiComponentVariables.SetComponentVariables(componentName, varDefinitions, varValues);
            }
        } 

        public List<Triple<string, string, string>> ParseVariables()
        {
            return this.uiComponentVariables.ParseVariables();
        }        
    }
}
